﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DBAccessLibrary.Common
{
    public static class StringEx
    {
        const string filterPattern = "'.*?'";           //查找出‘ ‘ 里的数据
        const string SqlFilterKeyword = "_&&__&*&*";    //替换字符
        public static IEnumerable<string> ToFilterKeyword(this string content,string keyword)
        {
            //查找出所有 ‘ ’里的内容
            if (Regex.IsMatch(content, filterPattern))
            {
                foreach (Match m in Regex.Matches(content, filterPattern))
                {
                    if (m.Value.IndexOf(keyword) > 0)
                    {
                        string regStr = m.Value.Replace(keyword, SqlFilterKeyword);
                        content = content.Replace(m.Value, regStr);
                    }
                }
            }
            List<string> list = new List<string>();
            foreach (string s in content.Split(new string[] { keyword }, StringSplitOptions.RemoveEmptyEntries))
            {
                list.Add(s.Trim().Replace(SqlFilterKeyword,keyword));
            }
            return list;
        }
    }
}
